<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@include file="/include/public.jspf"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta name="format-detection" content="telephone=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no" />
<link rel="stylesheet" type="text/css" href="${path }/css/default/m-base.css" />
<link rel="stylesheet" type="text/css" href="${path }/css/default/m-common.css" />
<link rel="stylesheet" type="text/css" href="${path }/css/default/p-all.css" />
<script type="text/javascript" src="${path }/js/jquery1.10.2.min.js"></script>
<script type="text/javascript" src="${path }/js/lib/zepto.js"></script>
<script type="text/javascript" src="${path }/js/common/base.js"></script>
<script type="text/javascript" src="${path }/js/helper.js"></script>
<script type="text/javascript" src="${path }/js/member/invest.js"></script>
<title>魔环金融</title>
</head>
<body>
   <div class="ui_title">
        <a class="ui_back" href="javascript:void(0)" onclick="window.history.back();"><i class="ui_icon_back"></i>返回</a>
        投资确认</div>
    <section class="m-main p_deal">
        <div class="inv-header mb10">
            <div class="invh-title">${borrowInfo.borrowName}
               </div>
            <div class="f15">
                <p>${borrowInfo.borrowDuration}<c:if test="${borrowInfo.durType eq 'DAY'}">天</c:if>
															<c:if test="${borrowInfo.durType eq 'MONTH'}">个月</c:if> / 年化${borrowInfo.borrowRate}% / ${borrowInfo.repaymentType.text }</p>
                <p>剩余：<fmt:formatNumber value="${borrowInfo.borrowMoney / 100 - borrowInfo.raiseMoney / 100}" pattern="0"> </fmt:formatNumber>元</p>
            </div>
        </div>
        <div class="ui_bg_box bid_con mb10">
            <div class="bid_kt">可用余额：<fmt:formatNumber value="${account.baseMoney/100 - account.freezeMoney/100}" pattern="0.00"> </fmt:formatNumber>元<a href="${path }/pay/toimprest.htm">充值</a><a id="xf_con_inputfbjk" href="javascript:;">使用红包</a></div>
            <c:if test="${empty redPacket }">
            <div class="gray_v1">含红包金额：0元</div>
            </c:if>
            <c:if test="${not empty redPacket }">
            <div class="gray_v1">含红包金额：<fmt:formatNumber value="${redPacket.money/100}" pattern="0"/>元</div>
            <input type="hidden" id="chooseid" value="${redPacket.id }">
            </c:if>
            <div class="put_con">
                <div class="ui_ipt">
                    <input id="JS-money" type="tel" maxlength="9" placeholder="<fmt:formatNumber value="${borrowInfo.minInvest / 100}" type="currency" pattern="0" />元起投" class="ui_input">
                    <input type="hidden" name="borrowInfoId" id="borrowInfoId" value="${borrowInfo.id}" />
                    <input type="hidden" name="borrowDuration" id="borrowDuration" value="${borrowInfo.borrowDuration}" />
                    <input type="hidden" name="borrowRate" id="borrowRate" value="${borrowInfo.borrowRate}" />
                    <input type="hidden" name="durType" id="durType" value="${borrowInfo.durType}" />
                    <input type="hidden" name="rewardRate" id="rewardRate" value="${borrowInfo.rewardRate}" />
                    <input type="hidden" name="remain" id="remain" value="<fmt:formatNumber value="${account.baseMoney/100 - account.freezeMoney/100}" pattern="0"/>" />
                    <input type="hidden" name="avaliable" id="avaliable" value="<fmt:formatNumber value="${borrowInfo.borrowMoney / 100 - borrowInfo.raiseMoney / 100}" pattern="0"/>" />
                    <input type="hidden" name="mini" id="mini" value="<fmt:formatNumber value="${borrowInfo.minInvest / 100}" pattern="0"/>" />
                </div>
                <div class="put_btn" id="all_bid_btn">全投</div>
            </div>
                        <div class="yq_money gray_v1 mt10">预期收益：--</div>
                    </div>
        <%-- <div class="ui_bg_box mb10">
                <div class="ipt-list">
                    <a class="JS-to_contlist" href="${path }/deal/contlist.jsp?dealid=59827&money=100" >合同和协议<i class="ui_arrow fr"></i></a>
                </div>
        </div> --%>
        <div class="ui-deal-btn">
            <input id="JS-submit" type="button" value="同意合同和协议并投资" class="ui_btn_red">
        </div>
    </section>
        <script>
    var mini = $("#min").val();
    var avaliable = $("#avaliable").val();
    var remain = $("#remain").val();
    var _couponIsFixed = 0;
    var userCoupon = "";
    var couponRate = "";
    var _inBidRequest = false;

    //var bidCacheJSON = P2PWAP.cache.get('_PAGE_DEAL_BID_formcache_'+productID);
    //bidBackFill(bidCacheJSON);
    function bidBackFill(json) {
        if (typeof json != 'undefined') {
            var bidCacheObj = JSON.parse(json);
            var money = bidCacheObj.money;
            var fcode = bidCacheObj.fcode;
            var fcodeRate = bidCacheObj.fcodeRate;
            if (money != ''){
                $('#JS-money').val(money);
                calcMoney();
            } 
            if (_couponIsFixed) return;
            if (fcode != '') {
                $('#JS-bindCouponStr').html(fcode);
                $('#JS-fcode').val(fcode);
            }
            if (fcodeRate != '') {
                $('.p_deal_fcode_panel .fcode_detail').html('使用邀请码投资可获额外年化' + fcodeRate + '%的返利，返利的计算及发放时间参见具体项目详情页内的规则描述');
            }
        }
    }
    function upateSubmitButton() {
        var disabled = _inBidRequest || $('#JS-money').val() == '';
        if (disabled) {
            $('#JS-submit').attr('disabled', 'disabled');
        } else {
            $('#JS-submit').removeAttr('disabled');
        }
    }
    var _upateSubmitButtonCount = 10;
    var _upateSubmitButtonTimer = setInterval(function(){
        upateSubmitButton();
        _upateSubmitButtonCount--;
        if (_upateSubmitButtonCount < 0) {
            clearInterval(_upateSubmitButtonTimer);
        }
    }, 300);
    $('#all_bid_btn').click(function(){
        $('#JS-money').val(Math.min(avaliable, remain));
        upateSubmitButton();
        calcMoney();
        bidCache();
    });
    $('#JS-money').bind('input',function(){
        upateSubmitButton();
        calcMoney();
        bidCache();
    });

    function calcMoney(){
        var money = $("#JS-money").val() * 1;
        if (money == null || money == "") {
			$(".yq_money").html('预期收益：--');
			return;
		}
		if (isNaN(money)) {
			$(".yq_money").html('预期收益：--');
			return;
		}
		var re = /^[1-9]\d*$/;
		if (!re.test(money)) {
			$(".yq_money").html('预期收益：--');
	        return;
	    }
		var borrowrate = parseFloat($("#borrowRate").val()/100);
		var rewardRate = parseFloat($("#rewardRate").val()/100);
		var borrowDuration = parseFloat($("#borrowDuration").val());
		var durType = $("#durType").val();
		var espectMoney = 0;
		//alert("money:" + money);
		//alert("borrowrate:" + borrowrate);
		//alert("rewardRate:" + rewardRate);
		//alert("borrowDuration:" + borrowDuration);
		if (durType == "MONTH") {
			espectMoney = money * (borrowrate+rewardRate) / 12 * borrowDuration;
		} else if (durType == "DAY") {
			espectMoney = money * (borrowrate+rewardRate) / 365 * borrowDuration;
		}
        $('.yq_money').html('预期收益：' + espectMoney.toFixed(2) + '元');
    }
    function changeContraLink(){
                
        /* var money = $("#JS-money").val() * 1;
        if (!(money > 0)) return;
        //合同
        $('.JS-to_contlist').attr('href','/deal/contlist?dealid='+ productID + '&money='+ money); */
    }

    function bidCache() {
        /*var _bidBackFillDate = {
            'fcode': userCoupon,
            'money': $("#JS-money").val(),
            'fcodeRate': couponRate
        }
        P2PWAP.cache.set('_PAGE_DEAL_BID_formcache_'+productID,JSON.stringify(_bidBackFillDate));*/
    } 
    //邀请码面板
    var _fcodePanelIsShow = false;
    var _inFcodeRequest = false;
    $('#Js-show_fcode_panel').click(function(){
        if (_fcodePanelIsShow) return;
        upateFcodeButton();
        _fcodePanelIsShow = true;
        document.body.scrollTop = 0;
        $('.p_deal_fcode_panel').addClass('p_show');
    });
    $('.p_deal_fcode_panel .ui_back').click(function(){
        closeFcodePanel();
    });
    function closeFcodePanel() {
        $('.p_deal_fcode_panel').removeClass('p_show');
        setTimeout(function(){
            _fcodePanelIsShow = false;
        },600);
    }
    function upateFcodeButton() {
        var disabled = _inFcodeRequest || $('#JS-fcode').val() == '';
        if (disabled) {
            $('#JS-fcode_submit').attr('disabled', 'disabled');
        } else {
            $('#JS-fcode_submit').removeAttr('disabled');
        }
    }
    $('#JS-fcode').bind('input',function(){
        upateFcodeButton();
    });
    $('#JS-fcode_submit').bind('click',function(){
        if (_couponIsFixed) {
            closeFcodePanel();
            return;
        }
        var couponVal = $.trim($('#JS-fcode').val());
        if (!/^[a-zA-Z0-9]+$/.test(couponVal)) {
            upateFcodeButton();
            P2PWAP.ui.toast('邀请码格式错误！');
            return;
        } else {
            if (_inFcodeRequest) return;
            _inFcodeRequest = true;
            upateFcodeButton();
            P2PWAP.util.request('/account/ajax_check_fcode',
                function(rawData){
                    _inFcodeRequest = false;
                    upateFcodeButton();
                    $('#JS-fcode').after('<i class="fcode_right">');
                    $('.p_deal_fcode_panel .fcode_detail').html('使用邀请码投资可获额外年化' + rawData.rebateRatio + '%的返利，返利的计算及发放时间参见具体项目详情页内的规则描述');
                    userCoupon = couponVal;
                    couponRate = rawData.rebateRatio;
                    _couponIsFixed = true;
                    $('#JS-bindCouponStr').html(userCoupon);
                    bidCache();
                  //面板不可用
                    $('#JS-fcode').attr('disabled', 'disabled').val(userCoupon);
                    $('.p_deal_fcode_panel .ui_ipt').addClass('ui_disabled');
                    $('#JS-fcode_submit').val('确定');
                    closeFcodePanel();
                },
                function(errorMsg){
                    _inFcodeRequest = false;
                    upateFcodeButton();
                    P2PWAP.ui.toast(errorMsg);
                },
                'get', {
                    'invite_code': couponVal
                });
        }
    });
    
    $(document).ready(function() {
		//点击使用红包
		$("#xf_con_inputfbjk").click(function() {
			var borrowInfoId = $("#borrowInfoId").val();
			var isLogin = ${isLogin};
			if(!isLogin){
				window.location.href="/page/login/login.jsp";
			}
			window.location.href="${path}/redpacket/getRedpacket.htm?borrowId="+borrowInfoId;
		});
	});
        </script>
</body>
</html>